package com.eascs.workflow.wf.dao;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.workflow.common.dao.BaseDao;
import com.eascs.workflow.common.entity.wf.ProcessTaskNode;

@Repository
public class ProcessTaskNodeDao extends BaseDao {
	private static final String TABLE_NAME = "wf_process_task_node";

	/**
	 * 获取流程节点列表分页
	 * 
	 * @param referId
	 * @return
	 */
	public PageResult<ProcessTaskNode> getTaskNodePageByReferId(String referId, PageResult<ProcessTaskNode> page) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from wf_process_task_node tn ");
		sql.append("	where tn.task_id in (");
		sql.append("	select task_id from wf_process_task_main where refer_id= ?");
		sql.append("	)");
		SqlPageQuery<ProcessTaskNode> pageQuery = new SqlPageQuery<ProcessTaskNode>(ProcessTaskNode.class,
				page.getPageSize(), page.getPageIndex(), sql.toString());
		return workflowJdbcSession.findDataWithPage(pageQuery);
	}

	/**
	 * 获取流程节点列表
	 * 
	 * @param referId
	 * @return
	 */
	public List<ProcessTaskNode> getTaskNodeListByReferId(String referId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from wf_process_task_node tn ");
		sql.append("	where tn.task_id in (");
		sql.append("	select ptm.task_id from wf_process_task_main ptm where refer_id = ?");
		sql.append("	)");
		return workflowJdbcSession.findEntitys(sql.toString(), ProcessTaskNode.class, referId);
	}

	/**
	 * 获取单据号下流程节点列表
	 * 
	 * @param referId
	 * @return
	 */
	public List<ProcessTaskNode> getTaskApprovalNodeList(String referId, String processId, String taskId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select tn.* ");
		sql.append("from wf_process_task_node tn,wf_process_task_main tm ");
		sql.append("	where tn.task_id = tm.task_id");
		sql.append("	and tm.refer_id = ? and tm.process_id = ? and tm.task_id = ? ");
		sql.append("	ORDER BY node_seq");
		return workflowJdbcSession.findEntitys(sql.toString(), ProcessTaskNode.class, referId, processId, taskId);
	}

	public List<ProcessTaskNode> getTaskNode(String taskMainId, Integer status) {
		String sql = "select * from " + TABLE_NAME + " where task_id = ? and node_status = ?";
		return workflowJdbcSession.findEntitys(sql, ProcessTaskNode.class, taskMainId, status);
	}

	/**
	 * 查询流程下所有节点
	 * 
	 * @param taskId
	 * @return
	 */
	public List<ProcessTaskNode> getNodeListByTaskId(String taskId) {
		String sql = "select * from " + TABLE_NAME + " where task_id = ? order by node_seq ";
		List<ProcessTaskNode> taskNodeList = workflowJdbcSession.findEntitys(sql, ProcessTaskNode.class, taskId);
		sortNodeTemplate(taskNodeList);
		return taskNodeList;
	}

	/**
	 * 根据节点父ID，获取当前节点
	 * 
	 * @param nodeParentId
	 * @return
	 */
	public ProcessTaskNode getNodeByParentId(String nodeParentId) {
		String sql = "select * from " + TABLE_NAME + " where node_parent_id = ?";
		ProcessTaskNode taskNodeList = workflowJdbcSession.findEntity(sql, ProcessTaskNode.class, nodeParentId);
		return taskNodeList;
	}

	/**
	 * 流程节点排序
	 * 
	 * @param nodeTemplateList
	 */
	private void sortNodeTemplate(List<ProcessTaskNode> taskNodeList) {
		Collections.sort(taskNodeList, new Comparator<ProcessTaskNode>() {
			public int compare(ProcessTaskNode o1, ProcessTaskNode o2) {
				return o1.getNodeSeq() - o2.getNodeSeq();
			}
		});
	}

	/**
	 * 修改节点类型
	 * 
	 * @param nodeId
	 * @param nodeType
	 * @return
	 */
	public int updateNodeType(String taskNodeId, String nodeType) {
		String sql = "update " + TABLE_NAME + " set node_type = ? where task_node_id = ?";
		return workflowJdbcSession.execute(sql, nodeType, taskNodeId);
	}

	/**
	 * 修改节点状态
	 * 
	 * @param nodeId
	 * @param nodeType
	 * @return
	 */
	public int updateNodeStatus(String taskNodeId, Integer nodeStatus, Date auditorEndTime) {
		String sql = "update " + TABLE_NAME + " set node_status = ?,auditor_end_date = ? where task_node_id = ?";
		return workflowJdbcSession.execute(sql, nodeStatus, auditorEndTime, taskNodeId);
	}

	public int deleteNodeByIds(List<String> nodeIdList) {
		String sql = "DELETE from wf_process_task_node where task_node_id in ";
		StringBuffer nodeIdBuffer = new StringBuffer("(");
		for (String nodeId : nodeIdList) {
			nodeIdBuffer.append("'").append(nodeId).append("',");
		}
		String nodeIdStr = nodeIdBuffer.substring(0, nodeIdBuffer.length() - 1) + ")";
		sql = sql + nodeIdStr;
		return workflowJdbcSession.execute(sql);
	}

	public ProcessTaskNode getTaskNodeDetail(String referId, String processType, String nodeCode) {
		String sql = "SELECT tn.* from wf_process_task_node tn,wf_process_task_main tm where tn.task_id = tm.task_id "
				+ " and tm.refer_id = ? and tm.process_type = ? and tm.process_status in (0,2) "
				+ " and tn.node_code = ? ORDER BY node_seq desc LIMIT 1 ";
		return workflowJdbcSession.findEntity(sql, ProcessTaskNode.class, referId, processType, nodeCode);
	}

	public ProcessTaskNode getTaskNodeByTaskIdAndNodeSeq(String taskId, int nodeSeq) {
		String sql = "SELECT * from wf_process_task_node t where t.task_id = ? and t.node_seq = ?";
		return workflowJdbcSession.findEntity(sql, ProcessTaskNode.class, taskId, nodeSeq);
	}

	public int updateTaskNodeStartDate(String taskNodeId, Date startDate) {
		String sql = "update " + TABLE_NAME + " set auditor_start_date = ? where task_node_id = ?";
		return workflowJdbcSession.execute(sql, startDate, taskNodeId);
	}
}
